package one.bugu.android.demon.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.renderscript.Sampler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.juefeng.android.framework.common.base.BaseFragment;
import com.juefeng.android.framework.common.base.BaseListAdapter;
import com.juefeng.android.framework.common.util.StringUtil;
import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.WarnBean;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.common.MyBaseFragment;
import one.bugu.android.demon.ui.activity.SetWarnActivity;
import one.bugu.android.demon.util.ColorUtil;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.ParamUtils;
import one.bugu.android.demon.util.PreferencesUtil;


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * 预警主界面列表适配
 */
public class WarnAdapter extends BaseListAdapter<WarnBean> {

    private Context context;

    private MyBaseActivity fragment;

    DecimalFormat df = new DecimalFormat("0.00");

    public WarnAdapter(Context context, MyBaseActivity fragment) {
        super(context);
        this.context = context;
        this.fragment = fragment;
    }

    private String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    private String date;

    private String numPrice;

    /**
     * 人民币汇率
     */
    private String rmbPrice;

    public String getRmbPrice() {
        return rmbPrice;
    }

    public void setRmbPrice(String rmbPrice) {
        this.rmbPrice = rmbPrice;
    }

    public Double getRelativePrice() {
        return relativePrice;
    }

    public void setRelativePrice(Double relativePrice) {
        this.relativePrice = relativePrice;
    }

    private Double relativePrice;

    @Override
    public int getContentLayoutId() {
        return R.layout.item_warn;
    }

    @Override
    public ViewHolder getViewHolder() {
        return new Holder();
    }

    public class Holder implements ViewHolder<WarnBean> {

        /**
         * 交易对
         */
        private TextView warnJyd;

        /**
         * 平台
         */
        private TextView warnPlatform;

        /**
         * 币名
         */
        private TextView warnCoinName;

        /**
         * 价格
         */
        private TextView warnPrice;

        /**
         * 涨幅
         */
        private Button warnIncreate;

        /**
         * 人民币价格
         */
        private TextView priceRmb;

        /**
         * 设置
         */
        private LinearLayout warnSetUp;

        /**
         * 设置图标
         */
        private ImageView warnSetUpIcon;

        /**
         * 涨幅为0时的文字
         */
        private TextView txtZero;

        /**
         * 省略号(...)
         */
        private TextView ignore;

        private String token;

        @Override
        public void initView(View view) {

            warnJyd = view.findViewById(R.id.item_warnJyd);
            warnPlatform = view.findViewById(R.id.item_warnPlatform);
            warnCoinName = view.findViewById(R.id.item_warnCoinName);
            warnPrice = view.findViewById(R.id.item_warnPrice);
            warnIncreate = view.findViewById(R.id.item_warnIncreate);

            warnSetUp = view.findViewById(R.id.item_warnSetUp);
            warnSetUpIcon = view.findViewById(R.id.item_warnSetUpIcon);

            txtZero = view.findViewById(R.id.txt_zero);
            ignore = view.findViewById(R.id.ignore);

            priceRmb = view.findViewById(R.id.price_rmb);


        }

        @SuppressLint("SetTextI18n")
        @Override
        public void initData(WarnBean warnBean) {



            warnJyd.setText(warnBean.getCoinName() + "/" + warnBean.getRelativeTrade());

            warnPlatform.setText(warnBean.getPlatformName());

            if (warnBean.getCoinNameCN().equals("")) {

                warnCoinName.setText("");
            } else {

                if (warnBean.getCoinNameCN().length() > 3){

                    ignore.setVisibility(View.VISIBLE);
                }else {

                    ignore.setVisibility(View.GONE);
                }

                warnCoinName.setText(warnBean.getCoinNameCN());
            }

            BigDecimal bigDecimal = new BigDecimal(warnBean.getPriceUsdt());

            warnPrice.setText(bigDecimal.toPlainString());

            DecimalFormat df1 = new DecimalFormat("#.######");
            Double num_priceUsdt = Double.parseDouble(bigDecimal.toPlainString());
            Double num_rmbRate = Double.parseDouble(StringUtil.isEmpty(rmbPrice)?"0":rmbPrice);

            Double num_rmbUsdt = num_priceUsdt * num_rmbRate;
            Double num_rmbOther;

            if (warnBean.getRelativePrice() != null){
                num_rmbOther = num_priceUsdt * num_rmbRate * warnBean.getRelativePrice();
            }else {

                num_rmbOther = num_priceUsdt * num_rmbRate;
            }


            if (!warnBean.getRelativeTrade().equals("USDT")){

                priceRmb.setText("¥" + df1.format(num_rmbOther));


            }else {

                priceRmb.setText("¥" + df1.format(num_rmbUsdt));
            }


            if (!StringUtil.isEmpty(warnBean.getPriceUpRatio())){
                try {
                    Double d = Double.parseDouble(warnBean.getPriceUpRatio());

                    if (d > 0) {
                        //涨幅大于0 显示上涨图片
                        warnIncreate.setText("+" + df.format(d*100) + "%");
                        warnIncreate.setBackgroundColor(Color.parseColor("#35BC78"));

                    } else if (d < 0){
                        //涨幅小于0 显示下跌图片
                        warnIncreate.setText(df.format(d*100) + "%");
                        warnIncreate.setBackgroundColor(Color.parseColor("#FE7361"));

                    }else {
                        //涨幅等于0 显示0.00%

                        if (Double.parseDouble(warnBean.getOpenPrice()) == 0 || warnBean.getOpenPrice() == null){

                            warnIncreate.setText("0.00%");
                            warnIncreate.setBackgroundColor(Color.parseColor("#C0C7C6"));
                        }else {

                            Double num1 = Double.parseDouble(warnBean.getPriceUsdt());
                            Double num2 = Double.parseDouble(warnBean.getOpenPrice());
                            Double num = (num1 - num2) / num2;

                            if (num > 0) {
                                warnIncreate.setText("+" + df.format(num * 100) + "%");
                                warnIncreate.setBackgroundColor(Color.parseColor("#35BC78"));

                            } else if (num < 0){
                                warnIncreate.setText(df.format(num * 100) + "%");
                                warnIncreate.setBackgroundColor(Color.parseColor("#FE7361"));

                            }else {

                                warnIncreate.setText("0.00%");
                                warnIncreate.setBackgroundColor(Color.parseColor("#C0C7C6"));
                            }
                        }

                    }
                }catch (Exception e){}
            }

            if (warnBean.getWarnFlag().equals("0")) {
                warnSetUpIcon.setImageResource(R.mipmap.weishezhi);
            } else {

                warnSetUpIcon.setImageResource(R.mipmap.yishezhi);
            }
        }

        @Override
        public void initEvent(final WarnBean warnBean) {

            //设置点击事件
            warnSetUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    PreferencesUtil.getInstance().putString(context, "relativeTrade", warnBean.getRelativeTrade());
                    PreferencesUtil.getInstance().putString(context, "priceUsdt", warnBean.getPriceUsdt());

                    List<ParamUtils.NameValue> parList = ParamUtils.build().put("platformName", warnBean.getPlatformName())
                            .put("coinName", warnBean.getCoinName()).put("relativeTrade", warnBean.getRelativeTrade())
                            .put("platformId", warnBean.getPlatformId()).put("coinId", warnBean.getCoinId())
                            .put("warnFlag", warnBean.getWarnFlag()).put("coinNameCN", warnBean.getCoinNameCN())
                            .create();
                    IntentUtils.startAtyForResult(fragment, SetWarnActivity.class,parList,1001);

                }
            });
        }

    }
}
